package com.alex.wechat.mp.message.news.service.impl;

import java.io.Serializable;
import java.util.List;

import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alex.wechat.mp.config.WxContext;
import com.alex.wechat.mp.message.news.entity.WxNewsTemplate;
import com.alex.wechat.mp.message.news.service.WxNewsTemplateServiceI;
import com.gomore.jeecg.common.Assert;

/**
 * @author Debenson
 * @since 0.1
 */
@Service("wxNewsTemplateService")
@Transactional
public class WxNewsTemplateServiceImpl extends CommonServiceImpl implements WxNewsTemplateServiceI {

  @Override
  public <T> void delete(T entity) {
    super.delete(entity);
    // 执行删除操作配置的sql增强
    this.doDelSql((WxNewsTemplate) entity);
  }

  @Override
  public <T> Serializable save(T entity) {
    WxNewsTemplate tpl = (WxNewsTemplate) entity;
    tpl.setAccountId(WxContext.getOriginalId());
    Serializable t = super.save(tpl);
    // 执行新增操作配置的sql增强
    this.doAddSql(tpl);
    return t;
  }

  @Override
  public <T> void saveOrUpdate(T entity) {
    super.saveOrUpdate(entity);
    // 执行更新操作配置的sql增强
    this.doUpdateSql((WxNewsTemplate) entity);
  }

  /**
   * 默认按钮-sql增强-新增操作
   * 
   * @param id
   * @return
   */
  public boolean doAddSql(WxNewsTemplate t) {
    return true;
  }

  /**
   * 默认按钮-sql增强-更新操作
   * 
   * @param id
   * @return
   */
  public boolean doUpdateSql(WxNewsTemplate t) {
    return true;
  }

  /**
   * 默认按钮-sql增强-删除操作
   * 
   * @param id
   * @return
   */
  public boolean doDelSql(WxNewsTemplate t) {
    return true;
  }

  @Override
  @Transactional(readOnly = true)
  public List<WxNewsTemplate> getAllByAccountId(String accountId) {
    Assert.assertArgumentNotEmpty(accountId, "accountId");

    String hql = "from " + WxNewsTemplate.class.getName()
        + " where accountId = ? order by templateName";
    return findByHql(hql, accountId);
  }

  /**
   * 替换sql中的变量
   * 
   * @param sql
   * @return
   */
  // public String replaceVal(String sql,NewsTemplate t){
  // sql = sql.replace("#{id}",String.valueOf(t.getId()));
  // sql = sql.replace("#{accountname}",String.valueOf(t.getAccountname()));
  // sql = sql.replace("#{accounttoken}",String.valueOf(t.getAccounttoken()));
  // sql = sql.replace("#{accountnumber}",String.valueOf(t.getAccountnumber()));
  // sql = sql.replace("#{accounttype}",String.valueOf(t.getAccounttype()));
  // sql = sql.replace("#{accountemail}",String.valueOf(t.getAccountemail()));
  // sql = sql.replace("#{accountdesc}",String.valueOf(t.getAccountdesc()));
  // sql = sql.replace("#{accountappid}",String.valueOf(t.getAccountappid()));
  // sql =
  // sql.replace("#{accountappsecret}",String.valueOf(t.getAccountappsecret()));
  // sql =
  // sql.replace("#{accountaccesstoken}",String.valueOf(t.getAccountaccesstoken()));
  // sql = sql.replace("#{UUID}",UUID.randomUUID().toString());
  // return sql;
  // }
}